import { Link } from 'expo-router'
import { openBrowserAsync } from 'expo-web-browser'
import { type ComponentProps } from 'react'
import { Platform } from 'react-native'
import { useThemeColor } from '@/hooks/useThemeColor'

type Props = Omit<ComponentProps<typeof Link>, 'href'> & { href: string }

export function ExternalLink({ href, style, ...rest }: Props) {
  const color = useThemeColor({}, 'link')
  return (
    <Link
      target="_blank"
      {...rest}
      href={href}
      onPress={async (event) => {
        if (Platform.OS !== 'web') {
          // 防止链接到本机默认浏览器的默认行为
          event.preventDefault()
          // 在应用内浏览器中打开链接。
          await openBrowserAsync(href)
        }
      }}
      style={[{ color }, style]}
    />
  )
}
